package com.reggie.controller;

import com.reggie.pojo.ShoppingCart;
import com.reggie.pojo.User;
import com.reggie.service.ShoppingCartService;
import com.reggie.util.ResultUtil;
import com.reggie.util.SnowflakeIdWorkerUtil;
import com.reggie.util.ThreadLocalUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.constraints.Null;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

//购物车请求映射类
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    ShoppingCartService shoppingCartService;
    @Autowired
    RedisTemplate redisTemplate;

    //获取该用户所有的购物车
    @GetMapping("/list")
    public ResultUtil<List<ShoppingCart>> list(){
        Map<String, Object> map = ThreadLocalUtil.get();
        List<ShoppingCart> shoppingCarts = shoppingCartService.findAll(Long.valueOf((String) map.get("id")));
        if (shoppingCarts==null) return ResultUtil.error("未查询到购物车信息！");
        return ResultUtil.success(shoppingCarts);
    }

    //添加购物车信息（Mysql）
    @PostMapping("/add")
    public ResultUtil<String> add(@RequestBody ShoppingCart shoppingCart){
        Map<String, Object> map = ThreadLocalUtil.get();
        shoppingCart.setUserId(Long.valueOf((String) map.get("id")));
        ShoppingCart shoppingCart1 = shoppingCartService.find(shoppingCart);
        if (shoppingCart1==null) {
            Long id = new SnowflakeIdWorkerUtil(0, 0).nextId();
            shoppingCart.setId(id);
            shoppingCart.setNumber(1);
            if (shoppingCartService.add(shoppingCart)) return ResultUtil.success("添加购物车信息成功！");
            return ResultUtil.error("添加购物车信息失败！");
        }
        shoppingCart.setNumber(shoppingCart1.getNumber()+1);
        if (shoppingCartService.edit(shoppingCart)) return ResultUtil.success("添加购物车信息成功！");
        return ResultUtil.error("添加购物车信息失败！");
    }

    //修改购物车信息
    @PostMapping("/sub")
    public ResultUtil<String> edit(@RequestBody ShoppingCart shoppingCart){
        Map<String, Object> map = ThreadLocalUtil.get();
        shoppingCart.setUserId(Long.valueOf((String) map.get("id")));
        ShoppingCart shoppingCart1 = shoppingCartService.find(shoppingCart);
        if (shoppingCart1.getNumber()<=1) {
            Long id = new SnowflakeIdWorkerUtil(0, 0).nextId();
            shoppingCart.setId(id);
            shoppingCart.setNumber(1);
            if (shoppingCartService.delete(shoppingCart)) return ResultUtil.success("编辑购物车信息成功！");
            return ResultUtil.error("编辑购物车信息失败！");
        }
        shoppingCart.setNumber(shoppingCart1.getNumber()-1);
        if (shoppingCartService.edit(shoppingCart)) return ResultUtil.success("编辑购物车信息成功！");
        return ResultUtil.error("添加购物车信息失败！");
    }

    //清空购物车
    @DeleteMapping("/clean")
    public ResultUtil<String> clean(){
        Map<String, Object> map = ThreadLocalUtil.get();
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(Long.valueOf((String) map.get("id")));
        if (shoppingCartService.delete(shoppingCart)) return ResultUtil.success("清空购物车成功！");
        return ResultUtil.error("清空购物车失败！");
    }
}
